package com.add.system;

import com.lineage.DatabaseFactory;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1FireSmithCrystalTable {
  private static final Log _log = LogFactory.getLog(L1FireSmithCrystalTable.class);
  
  private static L1FireSmithCrystalTable _instance;
  
  private final Map<Integer, L1FireCrystal> _FireCrystalIndex = new HashMap<>();
  
  public static L1FireSmithCrystalTable get() {
    if (_instance == null)
      _instance = new L1FireSmithCrystalTable(); 
    return _instance;
  }
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM system_firesmith_crystal");
      rs = pstm.executeQuery();
      while (rs.next()) {
        int itemid = rs.getInt("itemid");
        int enchant_lv0 = rs.getInt("enchant_lv0");
        int enchant_lv2 = rs.getInt("enchant_lv1");
        int enchant_lv3 = rs.getInt("enchant_lv2");
        int enchant_lv4 = rs.getInt("enchant_lv3");
        int enchant_lv5 = rs.getInt("enchant_lv4");
        int enchant_lv6 = rs.getInt("enchant_lv5");
        int enchant_lv7 = rs.getInt("enchant_lv6");
        int enchant_lv8 = rs.getInt("enchant_lv7");
        int enchant_lv9 = rs.getInt("enchant_lv8");
        int enchant_lv10 = rs.getInt("enchant_lv9");
        int enchant_lv11 = rs.getInt("enchant_lv10");
        int enchant_lv12 = rs.getInt("enchant_lv11");
        int enchant_lv13 = rs.getInt("enchant_lv12");
        int enchant_lv14 = rs.getInt("enchant_lv13");
        int enchant_lv15 = rs.getInt("enchant_lv14");
        L1FireCrystal fireCrystal = new L1FireCrystal(itemid, enchant_lv0, enchant_lv2, enchant_lv3, 
            enchant_lv4, enchant_lv5, enchant_lv6, enchant_lv7, enchant_lv8, enchant_lv9, enchant_lv10, 
            enchant_lv11, enchant_lv12, enchant_lv13, enchant_lv14, enchant_lv15);
        this._FireCrystalIndex.put(Integer.valueOf(itemid), fireCrystal);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    _log.info("载入火神融炼道具资料数量: " + this._FireCrystalIndex.size() + "(" + timer.get() + "ms)");
  }
  
  public L1FireCrystal getTemplate(int itemid) {
    return this._FireCrystalIndex.get(Integer.valueOf(itemid));
  }
}
